﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;

namespace component
{
    public partial class DocxBuilderComponent
    {
        public static List<OpenXmlElement> returnDeveloperGlossaryElements(XmlNode doc)
        {
            List<OpenXmlElement> developerGlossaryElements = new List<OpenXmlElement>();

            //Добавляем название топика из списка топиков
            Paragraph pNameTopic = new Paragraph();
            ParagraphProperties pPrNameTopic = new ParagraphProperties();
            ParagraphStyleId pStyleNameTopic = new ParagraphStyleId() { Val = "NameTopic" };
            pPrNameTopic.Append(pStyleNameTopic);
            pNameTopic.Append(pPrNameTopic);
            Run rNameTopic = new Run();
            if (DocxBuilderComponent.isFirst)
            {
                rNameTopic.Append(new Text(DocxBuilderComponent.topicTitleAndId[DocxBuilderComponent.topicID]));
                pNameTopic.Append(rNameTopic);
                developerGlossaryElements.Add(pNameTopic);
                DocxBuilderComponent.isFirst = false;
            }
            else
            {
                Break brNameTopic = new Break() { Type = BreakValues.Page };
                rNameTopic.Append(brNameTopic);
                rNameTopic.Append(new Text(DocxBuilderComponent.topicTitleAndId[DocxBuilderComponent.topicID]));
                pNameTopic.Append(rNameTopic);
                developerGlossaryElements.Add(pNameTopic);
            }

            foreach (XmlNode node in doc)
            {
                switch (node.Name)
                {
                    case "title":
                        {
                            //добавляем заглавие
                            if (node.HasChildNodes)
                            {
                                Paragraph p = new Paragraph();
                                ParagraphProperties pPr = new ParagraphProperties();
                                ParagraphStyleId pStyleId = new ParagraphStyleId() { Val = "Title" };
                                pPr.Append(pStyleId);
                                p.Append(pPr);
                                foreach (OpenXmlElement r in elementPara(node))
                                    p.Append(r);
                                developerGlossaryElements.Add(p);
                            }
                        }
                        break;
                    case "glossary":
                        {
                            if (node.HasChildNodes)
                            {
                                foreach (XmlNode glossaryEntry in node)
                                {
                                    if (glossaryEntry.NodeType == XmlNodeType.Element && glossaryEntry.Name == "glossaryEntry")
                                    {
                                        foreach (XmlNode entry in glossaryEntry)
                                        {
                                            switch (entry.Name)
                                            {
                                                case "terms":
                                                    {
                                                        if (entry.HasChildNodes)
                                                        {
                                                            List<string> nameTerm = new List<string>();
                                                            foreach (XmlNode term in entry)
                                                            {
                                                                if (term.NodeType == XmlNodeType.Element && term.Name == "term")
                                                                {
                                                                    nameTerm.Add(term.InnerText);
                                                                }
                                                            }

                                                            Paragraph p = new Paragraph();
                                                            ParagraphProperties pPr = new ParagraphProperties();
                                                            ParagraphStyleId pStyleId = new ParagraphStyleId() { Val = "GlossaryTerm" };
                                                            pPr.Append(pStyleId);
                                                            p.Append(pPr);
                                                            int lastElement = nameTerm.Count - 1;
                                                            for (int i = 0; i < nameTerm.Count; i++)
                                                            {
                                                                Run r = new Run();
                                                                Text txt = new Text();
                                                                if (i == 0)
                                                                    txt.Text = nameTerm[i];
                                                                else
                                                                    txt.Text = ", " + nameTerm[i];
                                                                r.Append(txt);
                                                                p.Append(r);
                                                            }
                                                            developerGlossaryElements.Add(p);
                                                        }
                                                    }
                                                    break;

                                                case "definition":
                                                    {
                                                        foreach (OpenXmlElement el in blockElement(entry))
                                                            developerGlossaryElements.Add(el);
                                                    }
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        break;
                }
            }


            return developerGlossaryElements;
        }
    }
}
